﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PD.Web.DynamicData.Filters
{
    /// <summary>
    /// Boolean filter control.
    /// </summary>
    public partial class BooleanFilter : System.Web.DynamicData.QueryableFilterUserControl
    {
        /// <summary>
        /// Value displayed in case of value is null.
        /// </summary>
        private const string NullValueString = "[null]";

        /// <summary>
        /// Gets the data control that handles the filter inside the filter template.
        /// </summary>
        /// <returns>The data control that handles the filter.</returns>
        public override Control FilterControl
        {
            get
            {
                return this.DropDownList1;
            }
        }

        /// <summary>
        /// Gets the transformed <see cref="T:System.Linq.IQueryable"/> object from the source <see cref="T:System.Linq.IQueryable"/> object.
        /// </summary>
        /// <param name="source">The source <see cref="T:System.Linq.IQueryable"/> object.</param>
        /// <returns>
        /// The transformed <see cref="T:System.Linq.IQueryable"/> object from the source <see cref="T:System.Linq.IQueryable"/> object.
        /// </returns>
        public override IQueryable GetQueryable(IQueryable source)
        {
            string selectedValue = this.DropDownList1.SelectedValue;
            if (string.IsNullOrEmpty(selectedValue))
            {
                return source;
            }

            object value = selectedValue;
            if (selectedValue == NullValueString)
            {
                value = null;
            }

            if (this.DefaultValues != null)
            {
                this.DefaultValues[this.Column.Name] = value;
            }

            return BooleanFilter.ApplyEqualityFilter(source, this.Column.Name, value);
        }

        /// <summary>
        /// Handles the Init event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Init(object sender, EventArgs e)
        {
            if (!this.Column.ColumnType.Equals(typeof(bool)))
            {
                throw new InvalidOperationException(string.Format("A boolean filter was loaded for column '{0}' but the column has an incompatible type '{1}'.", Column.Name, Column.ColumnType));
            }

            if (!this.Page.IsPostBack)
            {
                this.DropDownList1.Items.Add(new ListItem("All", string.Empty));
                if (!this.Column.IsRequired)
                {
                    this.DropDownList1.Items.Add(new ListItem("[Not Set]", NullValueString));
                }

                this.DropDownList1.Items.Add(new ListItem("True", bool.TrueString));
                this.DropDownList1.Items.Add(new ListItem("False", bool.FalseString));

                // Set the initial value if there is one
                string initialValue = this.DefaultValue;
                if (!string.IsNullOrEmpty(initialValue))
                {
                    this.DropDownList1.SelectedValue = initialValue;
                }
            }
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the DropDownList1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.OnFilterChanged();
        }
    }
}
